home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!j-bg.demon.co.uk
- From: John Sargent <jb@j-bg.demon.co.uk>
- Newsgroups: comp.lang.c++
- Subject: Re: Binary Converter
- Date: Fri, 05 Apr 96 22:47:11 GMT
- Message-ID: <828744431snz@j-bg.demon.co.uk>
- References: <31618729.6BC22935@oberon.hs.gettysburg.edu>
- Reply-To: jb@j-bg.demon.co.uk
- X-NNTP-Posting-Host: j-bg.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.29
- X-Mail2News-Path: j-bg.demon.co.uk
-
- In article <31618729.6BC22935@oberon.hs.gettysburg.edu> ** none ** writes:
-
- > I am working on a binary converter function, and it isn't working
- > correctly. I am basically passing a number to be converted to the
- > function, then using if statements to see which place of the number
- > would be in, dividing, and then doing the same with the remainder, until
- > the ones place. Each time the number fits one of the if statements, it
- > copies a one to a string, and for every if it does not fit, it copies a
- > zero. Lastly it returns the converted binary number. Does anyone know a
- > better way of doing this? Or why mine would not be working (I would
- > include the source here, but I had some trouble, and had to reformat my
- > drive, so it is gone as of now). If you can help, please email me at
- > mnicastr@oberon.hs.gettysburg.edu
- > Thanks
-
- void ToBin(unsigned int in, char * out)
- {
- unsigned mask = 0x8000;
- int bit;
-
- *out = 0;
-
- for ( bit = 0; bit < 16; bit++)
- {
- if ( in & mask )
- strcat(out, "1");
- else
- strcat(out, "0");
-
- mask = mask >> 1;
- }
- }
-
-
- Regards,
- John Sargent
-